
  var userR = document.getElementById('usernameError');
  var pswR = document.getElementById('passwordError');
  var confirmR = document.getElementById('confirmPswError');

  function clearErrorText(){
    userR.style.display = 'none';
    pswR.style.display = 'none';
    confirmR.style.display = 'none';
  }

  $(document).ready(function(){
    var currencyFileHref = hostUrl + "/lab/getlabtype";
    // 先进行请求.请求实验室相关数据 并显示在注册列表中.
    request(null,currencyFileHref,function(data){
      var showList = document.getElementById("labList");
      var labData = data.data;
      var liStr = '';
      for(var i in labData){
        if(!labData[i].open){
          liStr += "<option disabled='disabled'>" + labData[i].labid + " - " + labData[i].labname + "</option>";
        }else {
          liStr += "<option>" + labData[i].labid + " - " + labData[i].labname + "</option>";
        }
      }
      showList.innerHTML = liStr;
    },
    function(error){
      console.log(error);
    });

    // 检查注册信息合法性 .
    function checkInput(username,password,confirmpassword,labSelect){
      var userNameReg = /^([a-zA-Z0-9]{8,16})$/; // 8-16位学工号
      if(!userNameReg.test(username)){
        userR.innerHTML = "用户名不合法，请重新输入！";
        userR.style.display = 'block';
        return ;
      }else {
        userR.style.display = '';
      }
      var pswReg = /^([a-zA-Z0-9]{6,16})$/;
      if(!pswReg.test(password)){
        pswR.innerHTML = "密码格式不正确，请重新输入！";
        pswR.style.display = 'block';
        return ;
      }else {
        pswR.style.display = '';
      }
      if(password !== confirmpassword){
        confirmR.innerHTML = "两次输入密码不一致，请检查后输入！";
        confirmR.style.display = 'block';
        return ;
      }else {
        confirmR.style.display = '';
      }
    }

    // 确认提交事件
    document.getElementsByClassName('submitForm')[0]
            .onclick = function registerBtn(){
      var username = document.getElementById('userName').value,
          password = document.getElementById('psw').value,
          confirmpassword = document.getElementById('confirmPsw').value,
          labSelect = document.getElementById('labList');

      checkInput(username,password,confirmpassword,labSelect);

      var index = labSelect.selectedIndex;
      var selectLabStr = labSelect.options[index].text;
      var labid = selectLabStr.slice(0,1);

      var el = {};
      el.username = username,el.password = password,el.labid = labid;
      var reqUrl = hostUrl + '/account/register';
      request(el,reqUrl,function(data){
        if(data.status === 0){
          alert('注册成功！');
          var index = window.location.href.lastIndexOf('/');
          var str = window.location.href.slice(0,index);
          console.log(str);
          window.location.href = str;
        }
      },
      function(error){
        console.log(error);
      });
    }
  });
